import 'package:flutter/material.dart';

class Music extends StatefulWidget {
  const Music({super.key});

  @override
  State<Music> createState() => _MusicState();
}

class _MusicState extends State<Music> with SingleTickerProviderStateMixin{
  late AnimationController controller;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: const Center(
        child: SizedBox(
          height: 100,
          width: 30,
          child: SoundRecord(width: 7),
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
        },
        child: const Icon(Icons.play_arrow),
      ),
    );
  }

  @override
  void initState() {
    super.initState();
    controller = AnimationController(vsync: this, duration: const Duration(milliseconds: 600));
  }
}

class SoundRecord extends StatefulWidget {
  final double width;
  final double height;
  final Color color;
  final BorderRadiusGeometry borderRadius;
  final Duration duration;
  final Curve curve;


  const SoundRecord(
      {super.key,
        this.width = 7.0,
        this.height = 40.0,
        this.color = Colors.blue,
        this.borderRadius = const BorderRadius.all(Radius.circular(5)),
        this.duration = const Duration(milliseconds: 600),
        this.curve = Curves.linear});

  @override
  State<SoundRecord> createState() => _SoundRecordState();
}

class _SoundRecordState extends State<SoundRecord>
    with SingleTickerProviderStateMixin {
  late AnimationController _controller;
  late Animation _animation, _animation1, _animation2;
  List values = [
    [0.4, 0.8, 0.6, 0.4],
    [0.8, 0.3, 0.6, 0.8],
    [0.4, 0.7, 0.9, 0.4],
  ];
  bool _isAnimating = true;

  @override
  void initState() {
    _controller = AnimationController(vsync: this, duration: const Duration(milliseconds: 600))..repeat();

    _animation = TweenSequence([
      ...List.generate(3, (index) {
        return TweenSequenceItem(
            tween: Tween(begin: values[0][index], end: values[0][index + 1]),
            weight: 100.0 / values.length);
      })
    ]).animate(CurvedAnimation(parent: _controller, curve: widget.curve));

    _animation1 = TweenSequence([
      ...List.generate(3, (index) {
        return TweenSequenceItem(
            tween: Tween(begin: values[1][index], end: values[1][index + 1]),
            weight: 100.0 / values.length);
      })
    ]).animate(CurvedAnimation(parent: _controller, curve: widget.curve));

    _animation2 = TweenSequence([
      ...List.generate(3, (index) {
        return TweenSequenceItem(
            tween: Tween(begin: values[2][index], end: values[2][index + 1]),
            weight: 100.0 / values.length);
      })
    ]).animate(CurvedAnimation(parent: _controller, curve: widget.curve));

    super.initState();
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return AnimatedBuilder(
        animation: _controller,
        builder: (context, child) {
          return Row(
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Bar(
                color: widget.color,
                width: widget.width,
                borderRadius: widget.borderRadius,
                height: _animation.value * widget.height,
              ),
              Bar(
                color: widget.color,
                width: widget.width,
                borderRadius: widget.borderRadius,
                height: _animation1.value * widget.height,
              ),
              Bar(
                color: widget.color,
                width: widget.width,
                borderRadius: widget.borderRadius,
                height: _animation2.value * widget.height,
              ),
            ],
          );
        });
  }
}

class Bar extends StatelessWidget {
  final double width;
  final double height;
  final Color color;
  final BorderRadiusGeometry borderRadius;

  const Bar({
    super.key,
    required this.width,
    required this.height,
    this.color = Colors.white,
    required this.borderRadius,
  });

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: width,
      height: height,
      child: DecoratedBox(
        decoration: BoxDecoration(
          shape: BoxShape.rectangle,
          color: color,
          borderRadius: borderRadius,
        ),
      ),
    );
  }
}
